package com.ctshk.rpc.booking.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.ctshk.common.enums.IsDeletedCode;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.common.utils.SnowflakeIdWorker;
import com.ctshk.rpc.booking.dto.outside.food.query.SupplierOutsideOrderFoodDTO;
import com.ctshk.rpc.booking.dto.outside.food.query.SupplierOutsideOrderFoodDetailDTO;
import com.ctshk.rpc.booking.dto.outside.food.query.SupplierOutsideOrderFoodTouristDTO;
import com.ctshk.rpc.booking.entity.SupplierOutsideOrderFood;
import com.ctshk.rpc.booking.entity.SupplierOutsideOrderFoodDetail;
import com.ctshk.rpc.booking.entity.SupplierOutsideOrderFoodTourist;
import com.ctshk.rpc.booking.mapper.SupplierOutsideOrderFoodDetailMapper;
import com.ctshk.rpc.booking.mapper.SupplierOutsideOrderFoodMapper;
import com.ctshk.rpc.booking.mapper.SupplierOutsideOrderFoodTouristMapper;
import com.ctshk.rpc.booking.req.outside.food.save.SupplierOutsideOrderFoodReq;
import com.ctshk.rpc.booking.service.ISupplierOutsideOrderFoodService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 站外餐饮预订信息 服务实现类
 * </p>
 *
 * @author 谢诗宏
 * @since 2021-03-09
 */
@DubboService
public class SupplierOutsideOrderFoodServiceImpl implements ISupplierOutsideOrderFoodService {
    @Autowired
    SupplierOutsideOrderFoodMapper supplierOutsideOrderFoodMapper;
    @Autowired
    SupplierOutsideOrderFoodTouristMapper supplierOutsideOrderFoodTouristMapper;
    @Autowired
    SupplierOutsideOrderFoodDetailMapper supplierOutsideOrderFoodDetailMapper;

    @Override
    public void save(Long supplierOrderId, Long userId, LocalDateTime nowDateTime, List<SupplierOutsideOrderFoodReq> supplierOutsideOrderFoodReqList) {
        if (supplierOrderId != null) {
            //删除
            QueryWrapper<SupplierOutsideOrderFood> outsideOrderFoodWrapper = new QueryWrapper<>();
            outsideOrderFoodWrapper.lambda().eq(SupplierOutsideOrderFood::getOrderId, supplierOrderId);
            supplierOutsideOrderFoodMapper.delete(outsideOrderFoodWrapper);
            QueryWrapper<SupplierOutsideOrderFoodDetail> outsideOrderFoodDetailWrapper = new QueryWrapper<>();
            outsideOrderFoodDetailWrapper.lambda().eq(SupplierOutsideOrderFoodDetail::getOrderId, supplierOrderId);
            supplierOutsideOrderFoodDetailMapper.delete(outsideOrderFoodDetailWrapper);
            QueryWrapper<SupplierOutsideOrderFoodTourist> outsideOrderFoodTouristWrapper = new QueryWrapper<>();
            outsideOrderFoodTouristWrapper.lambda().eq(SupplierOutsideOrderFoodTourist::getFoodId, supplierOrderId);
            supplierOutsideOrderFoodTouristMapper.delete(outsideOrderFoodTouristWrapper);
        }
        //新增餐饮信息
        supplierOutsideOrderFoodReqList.forEach(outsideOrderFoodReq -> {
            SupplierOutsideOrderFood supplierOutsideOrderFood = EntityUtil.copy(outsideOrderFoodReq, SupplierOutsideOrderFood.class);
            supplierOutsideOrderFood.setId(SnowflakeIdWorker.nextId());
            supplierOutsideOrderFood.setOrderId(supplierOrderId);
            supplierOutsideOrderFood.setCreateId(userId);
            supplierOutsideOrderFood.setGmtCreate(nowDateTime);
            supplierOutsideOrderFood.setModifiedId(userId);
            supplierOutsideOrderFood.setGmtModified(nowDateTime);
            supplierOutsideOrderFood.setIsDeleted(IsDeletedCode.NO.getCode());
            supplierOutsideOrderFoodMapper.insert(supplierOutsideOrderFood);
            if (CollectionUtils.isNotEmpty(outsideOrderFoodReq.getSupplierOutsideOrderFoodDetailReqList())) {
                //新增餐饮详细信息
                outsideOrderFoodReq.getSupplierOutsideOrderFoodDetailReqList().forEach(outsideOrderFoodDetailReq -> {
                    SupplierOutsideOrderFoodDetail supplierOutsideOrderFoodDetail = EntityUtil.copy(outsideOrderFoodDetailReq, SupplierOutsideOrderFoodDetail.class);
                    supplierOutsideOrderFoodDetail.setId(SnowflakeIdWorker.nextId());
                    supplierOutsideOrderFoodDetail.setOrderId(supplierOrderId);
                    supplierOutsideOrderFoodDetail.setFoodId(supplierOutsideOrderFood.getId());
                    supplierOutsideOrderFoodDetail.setCreateId(userId);
                    supplierOutsideOrderFoodDetail.setGmtCreate(nowDateTime);
                    supplierOutsideOrderFoodDetail.setModifiedId(userId);
                    supplierOutsideOrderFoodDetail.setGmtModified(nowDateTime);
                    supplierOutsideOrderFoodDetail.setIsDeleted(IsDeletedCode.NO.getCode());
                    supplierOutsideOrderFoodDetailMapper.insert(supplierOutsideOrderFoodDetail);
                    
                });
            }
            if (CollectionUtils.isNotEmpty(outsideOrderFoodReq.getSupplierOutsideOrderFoodTouristReqList())) {
                //新增餐饮旅客信息
                outsideOrderFoodReq.getSupplierOutsideOrderFoodTouristReqList().forEach(outsideOrderFoodTouristReq -> {
                    SupplierOutsideOrderFoodTourist supplierOutsideOrderFoodTourist = EntityUtil.copy(outsideOrderFoodTouristReq, SupplierOutsideOrderFoodTourist.class);
                    supplierOutsideOrderFoodTourist.setId(SnowflakeIdWorker.nextId());
                    supplierOutsideOrderFoodTourist.setOrderId(supplierOrderId);
                    supplierOutsideOrderFoodTourist.setFoodId(supplierOutsideOrderFood.getId());
                    supplierOutsideOrderFoodTourist.setCreateId(userId);
                    supplierOutsideOrderFoodTourist.setGmtCreate(nowDateTime);
                    supplierOutsideOrderFoodTourist.setModifiedId(userId);
                    supplierOutsideOrderFoodTourist.setGmtModified(nowDateTime);
                    supplierOutsideOrderFoodTourist.setIsDeleted(IsDeletedCode.NO.getCode());
                    supplierOutsideOrderFoodTouristMapper.insert(supplierOutsideOrderFoodTourist);
                });
            }
        });
    }

    @Override
    public List<SupplierOutsideOrderFoodDTO> selectByOrderId(Long id) {
        List<SupplierOutsideOrderFoodDTO> supplierOutsideOrderFoodDTOS = new ArrayList<>();
        //查询机票列表
        QueryWrapper<SupplierOutsideOrderFood> foodWrapper = new QueryWrapper();
        foodWrapper.lambda().eq(SupplierOutsideOrderFood::getOrderId, id);
        List<SupplierOutsideOrderFood> supplierOutsideOrderFoods = supplierOutsideOrderFoodMapper.selectList(foodWrapper);
        if (CollectionUtils.isNotEmpty(supplierOutsideOrderFoods)) {
            supplierOutsideOrderFoodDTOS = EntityUtil.copyList(supplierOutsideOrderFoods, SupplierOutsideOrderFoodDTO.class);
            supplierOutsideOrderFoodDTOS.forEach(food -> {
                //查询餐饮详细列表
                QueryWrapper<SupplierOutsideOrderFoodDetail> foodFoodDetailWrapper = new QueryWrapper();
                foodFoodDetailWrapper.lambda().eq(SupplierOutsideOrderFoodDetail::getOrderId, id);
                foodFoodDetailWrapper.lambda().eq(SupplierOutsideOrderFoodDetail::getFoodId, food.getId());
                List<SupplierOutsideOrderFoodDetail> supplierOutsideOrderFoodDetails = supplierOutsideOrderFoodDetailMapper.selectList(foodFoodDetailWrapper);
                if (CollectionUtils.isNotEmpty(supplierOutsideOrderFoodDetails)) {
                    List<SupplierOutsideOrderFoodDetailDTO> supplierOutsideOrderFoodDetailDTOList = EntityUtil.copyList(supplierOutsideOrderFoodDetails, SupplierOutsideOrderFoodDetailDTO.class);
                    food.setSupplierOutsideOrderFoodDetailDTOList(supplierOutsideOrderFoodDetailDTOList);
                }
                //查询餐饮旅客列表
                QueryWrapper<SupplierOutsideOrderFoodTourist> foodTouristWrapper = new QueryWrapper();
                foodTouristWrapper.lambda().eq(SupplierOutsideOrderFoodTourist::getOrderId, id);
                foodTouristWrapper.lambda().eq(SupplierOutsideOrderFoodTourist::getFoodId, food.getId());
                List<SupplierOutsideOrderFoodTourist> supplierOutsideOrderFoodTourists = supplierOutsideOrderFoodTouristMapper.selectList(foodTouristWrapper);
                if (CollectionUtils.isNotEmpty(supplierOutsideOrderFoodTourists)) {
                    List<SupplierOutsideOrderFoodTouristDTO> supplierOutsideOrderFoodTouristDTOList = EntityUtil.copyList(supplierOutsideOrderFoodTourists, SupplierOutsideOrderFoodTouristDTO.class);
                    food.setSupplierOutsideOrderFoodTouristDTOList(supplierOutsideOrderFoodTouristDTOList);
                }
            });
        }
        return supplierOutsideOrderFoodDTOS;
    }
}
